<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 会员（网站成员）函数库
 * 
 * @desc 
 * @version 1.0.0 
 * @author  Jason
 * @date	2017年6月14日 下午4:57:20
 */
class Lib_member extends Lib_database
{
	/** @var Mod_member */
	private $mod_member = null;
	
	use libraries\common\DatabaseCURD;
	
	use libraries\common\ConditionParser;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->CI->load->model('mod_member');
		$this->mod_member =& $this->CI->mod_member;
		
		$this->set_render_config();
	}
	
	private function set_render_config()
	{
		$this->CI->load->model('mod_role');
		$foreign_keys = [
				'master' => [
						['role_id', ['name'], $this->CI->mod_role, 'master', 'id'],
				],
				'info' => [
				],
		];
		$time_fields = [
				'master' => [
						['create_time', 'Y-m-d H:i:s'],
				],
				'info' => [
				],
		];
		$encode_fields = [
				'master' => [
				],
				'info' => [
						['preferences', 'serialize'],	// 偏好设置
				],
		];
		$this->foreign_keys = $foreign_keys;
		$this->time_fields = $time_fields;
		$this->encode_fields = $encode_fields;
	}
	
	public function get_member_for_detail($id)
	{
		$item = $this->mod_member->select_instance_by_id($id);
		$info = $this->mod_member->select_instance_by_id($id, 'info');
		if (empty($item) || empty($info)) {
			return [];
		}
		$item = $this->render_list($item, 'master', true, [
				[$this, 'parse_member_item'],
		]);
		$info = $this->render_list($info, 'info', true);
		$item = array_merge($item, $info);
		return $item;
	}
	
	
	public function get_members_for_list($conditions, $page = 1, $number = 12)
	{
		$data = $this->mod_member->get_list($conditions, $page, $number);
		$data['data'] = $this->render_list($data['data'], 'master', false, [
				[$this, 'parse_member_item'],
		]);
		return $data;
	}
	
	public function parse_member_item($item)
	{
		$item['password'] = null;
		$item['status_tags'] = $this->mod_member->calculate_status_tags($item['status']);
		return $item;
	}
	
	/**
	 * 用户状态切换
	 *
	 * @author  Jason
	 * @date	2017年6月16日 下午12:13:34
	 *
	 * @param number $id
	 * @param string $positiveTag eg: 'unlimited', 'checked', 'activated', 'enabled'
	 *
	 * @return [<status>, <content>]	一个两元素数组，第一个元素表示状态，第二个表示相应内容
	 * 	        eg:	[true, ['nowTag' => 'enabled']]
	 * 				[true, ['nowTag' => 'disabled']]
	 * 				[false, ['error']]
	 */
	public function toggle_status_positive_tag()
	{
		$this->CI->load->library('form_validation');
		
		$status = false;
		$content = [];
		if(FALSE === $this->CI->form_validation->run("toggle_status_positive_tag")) {
			$errorKey = $this->CI->form_validation->error_array();
			$status = false;
			$content = $errorKey;
		} else {
			$id = $this->CI->input->post('id');
			$positiveTag = $this->CI->input->post('tag');
			
			$status = true;
			$ultimateTag = $this->mod_member->toggle_status_by_tag($id, $positiveTag);
			$content = ['nowTag' => $ultimateTag];
		}
		
		return [$status, $content];
	}
	
	public function create_member_with_info()
	{
		$this->CI->load->library('form_validation');
		// MARK 验证所需库函数对象 "懒加载"，详情参见 form_validation.php MaRK 用户管理验证规则 部分。
		// 把有时候必须加载的库函数放到最后 - 延迟加载
		$this->CI->load->library('lib_role');
		
		$status = false;
		$content = [];
		if(FALSE === $this->CI->form_validation->run("member_with_info_create")) {
			$errorKey = $this->CI->form_validation->error_array();
			$status = false;
			$content = $errorKey;
		} else {
			$data = $this->CI->input->post();
			
			list($status, $content) = $this->mod_member->create_member_with_info($data);
			if (!$status) {
				$content = ['faild'];
			}
		}
		
		return [$status, $content];
	}
	
	public function update_member_with_info()
	{
		$this->CI->load->library('form_validation');
		$this->CI->load->library('lib_role');
		
		$status = false;
		$content = [];
		if(FALSE === $this->CI->form_validation->run("member_with_info_update")) {
			$errorKey = $this->CI->form_validation->error_array();
			$status = false;
			$content = $errorKey;
		} else {
			$data = $this->CI->input->post();
			
			list($status, $content) = $this->mod_member->update_member_with_info($data);
			if (!$status) {
				$content = ['faild'];
			}
		}
		
		return [$status, $content];
	}
	
	/**
	 * 修改用户密码 - 管理员操作，高级权限：不需要原密码验证！
	 *
	 * @method  POST
	 * @author  Jason
	 * @date	2017年6月16日 下午5:46:05
	 * @param   $opassword, $password, $passconf
	 *
	 * @return  [<status>, <content>]	一个两元素数组，第一个元素表示状态，第二个表示相应内容
	 * 	        eg:	[true, <userData>]
	 * 				[false, <errorMesg>]
	 */
	public function update_member_password()
	{
		$this->CI->load->library('form_validation');
		
		$status = false;
		$content = [];
		if(FALSE === $this->CI->form_validation->run('member_password_update')) {
			$errorKey = $this->CI->form_validation->error_array();
			$status = false;
			$content = $errorKey;
		} else {
			$data = $this->CI->input->post();
			$member_id = $data['id'];
			$password = $data['password'];
			
			$status = $this->mod_member->update_member_password($member_id, $password);
		}
		
		return [$status, $content];
	}
	
	
}